<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: goodsearch.php 5677 2011-03-30 01:31:05Z menglingmin $
 */

define("CURSCRIPT", "goods");
include_once './common.php';

$active['goods'] = ' class="active"';
$viewby = (in_array($_G['cookie']['list_goods_view'], array('list', 'pic'))) ? $_G['cookie']['list_goods_view'] : 'pic';
$sortby = (in_array($_G['cookie']['list_goods_sort'], array('view', 'ask'))) ? $_G['cookie']['list_goods_sort'] : '';

$tagarrs = $goodlist = $where = $value = $attarray = array();
$tagids = $joinsql = $wheresql = '';
$query = NULL;

//常见id处理
foreach(array('itemid', 'nid', 'uid', 'catid', 'tagid', 'shopid') as $value) {
	$_GET[$value] = $_POST[$value] = intval(!empty($_POST[$value])?$_POST[$value]:$_GET[$value]);
}
$keyword = trim($_G['gp_keyword']);

$catid = empty($_GET['catid']) ? 0 : $_GET['catid'];

$categorylist = getcategory('good'); //读商品分类

include_once('./batch.attribute.php');
$searchcats = getsearchcats($categorylist, $catid);

// 分类ID搜索条件
if(!empty($catid)) {
	$tagid[] = $catid;
	if(is_array($searchcats)) {
		foreach($searchcats as $key=>$value) {
			$tagid[] = $value['catid'];
		}
	}
	$tagids = implode(",", $tagid);
	$where[] = 'i.catid IN (' . $tagids . ')';
} else {
	$where[] = 'i.catid > 0';
}

// 属性筛选器
$attrvalues = empty($_GET['params'])?array():getattrvalues($_GET['params']);
if($catid && $categorylist[$catid]['havechild'] == 0) {
	$attarray = formatattarr($catid, $attrvalues, $keyword);
}

// 属性搜索
$attr_in = getattr_in($catid, $attrvalues);

// 条件拼合
$attr_in!==NULL && $where[] = $attr_in;
$where[] = 'i.grade_s > 2 AND i.grade > 2';
$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
if($catid>0 && $tagids) {
	$where[] = 'i.catid IN ('.$tagids.')';
} else {
	$where[] = 'i.catid>0';
}
if($sortby == "view") {
	$sql_orderby = ' ORDER BY i.viewnum DESC';
} elseif($sortby == "ask") {
	$sql_orderby = ' ORDER BY i.replynum DESC';
} else {
	$sql_orderby = " ORDER BY i.displayorder ASC, i.itemid DESC";
}

$wheresql = implode(' AND ', $where);

// 分页处理
$tpp = $_G['setting']['goodsearchperpage'];

// 查询分类结果
if(empty($catid) || $categorylist[$catid]['havechild']) {
	$_BCACHE->cachesql('catnums', 'SELECT COUNT(i.itemid) as count, i.catid FROM '.DB::table('gooditems').' i WHERE '.$wheresql.' GROUP BY i.catid', 0, 0, 100, 0, 'sitelist', 'good');
	foreach($_SBLOCK['catnums'] as $value) {
		$catnums[$value['catid']] = intval($value['count']);
	}
	if(is_array($searchcats)) {
		foreach($searchcats as $key=>$value) {
			$catnums = getcatcount($value['catid'], $catnums);
		}
	}
}

// 数据查询，拆分SQL，分片缓存
$_BCACHE->cachesql('goodsearch', 'SELECT i.itemid, i.shopid FROM '.DB::table('gooditems').' i WHERE '.$wheresql . $sql_orderby, 0, 1, $tpp, 0, 'sitelist', 'good');
$multipage = $_SBLOCK['goodsearch_multipage'];
$resultcount = $_SBLOCK['goodsearch_listcount'];
foreach($_SBLOCK['goodsearch'] as $value) {
	$value = $_BCACHE->getiteminfo('good', $value['itemid'], $value['shopid']);
	$value['shopinfo'] = $_BCACHE->getshopinfo($value['shopid']);
	$value['intro'] = cutstr($value['intro'], 22, true);
	if ($viewby == "list") {
		$value['summary'] = cutstr($value['subject'], 39, true);
	} else {
		$value['summary'] = cutstr($value['subject'], 28, true);
	}
	if($viewby == 'list') {
		$value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
	}
	$goodlist[] = $value;
}

// 用于模板中显示

$keyword_show = htmlspecialchars($keyword);

$active['cat'] = ' class="active"';
$location['name'] = (!$keyword_show ? '' : $keyword_show . ' - ') . $_G['setting']['site_nav']['goods']['name'];

// 分类面包屑导航
require_once(B_ROOT.'./source/function/nav.func.php');
$catnavarray = getcatnavarray($catid, 'good');
$samelevelcatarray = getcategorychild($catid, 'good', $catid);

$seo_title = ($catid == 0 ? "" : $categorylist[$catid]['name'] . " - ") . $location['name'] . " - " . $seo_title;
include template('templates/site/default/goodsearch.html.php', 1);

ob_out();

?>